﻿using CoreModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Yuwell.AtomizerWeight.App.Views
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            ///
            var info = CoreModule.LocalConfigurationInfo.Load();

            info.PortName = this.cbPortNames.Text;
            info.WorkMode = this.cbWorkMode.Text;

            info.Save();



            try
            {
                CoreModule.Storages.MySqlDbStorage dbStorage = new CoreModule.Storages.MySqlDbStorage();

                var vaporInterval = Math.Abs(this.txtVaporInterval.Value) == 0 ? 1 : Math.Abs(this.txtVaporInterval.Value);
                var vaporLowValue = Math.Abs(this.txtVaporLowValue.Value);
                var vaporHighValue = Math.Abs(this.txtVaporHighValue.Value);

                dbStorage
                    .MeasurementArgs
                    .Add(new CoreModule.MeasurementArg()
                    {
                        VaporInterval = (int)vaporInterval,
                        VaporLowValue = Math.Min(vaporLowValue, vaporHighValue),
                        VaporHighValue = Math.Max(vaporLowValue, vaporHighValue),
                        ClientId = CoreModule.Extension.GetClientId(),
                    });
                dbStorage.SaveChanges();
            }
            catch (Exception ex)
            {
                var kk = 0;
            }
            this.DialogResult = true;
            this.Close();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbWorkMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbWorkMode.SelectedValue.ToString() == WorkMode.步骤二.ToString())
            {

                this.txtVaporInterval.IsEnabled = false;
                this.txtVaporLowValue.IsEnabled = false;
                this.txtVaporHighValue.IsEnabled = false;
            }
            else
            {
                this.txtVaporInterval.IsEnabled = true;
                this.txtVaporLowValue.IsEnabled = true;
                this.txtVaporHighValue.IsEnabled = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            var info = CoreModule.LocalConfigurationInfo.Load();

            var portNames = System.IO.Ports.SerialPort.GetPortNames()
                .OrderBy(item=>item)
                .ToList();

            //if (portNames.Count == 0)
            //{
            //    this.btnOk.IsEnabled = false;
            //}


            this.cbPortNames.ItemsSource = portNames;
            if (string.IsNullOrWhiteSpace(info.PortName))
            {
                this.cbPortNames.SelectedIndex = 0;
            }
            else
            {
                if (portNames.Contains(info.PortName))
                {
                    this.cbPortNames.Text = info.PortName;
                }
                else
                {
                    this.cbPortNames.SelectedIndex = 0;
                }

            }

            List<string> strings = new List<string>
            {
                CoreModule.WorkMode.步骤一.ToString(),
                CoreModule.WorkMode.步骤二.ToString(),
                CoreModule.WorkMode.步骤一和步骤二.ToString()
            };

            this.cbWorkMode.ItemsSource = strings;

            this.cbWorkMode.Text = info.WorkMode;

            try
            {

                CoreModule.Storages.MySqlDbStorage dbStorage = new CoreModule.Storages.MySqlDbStorage();

                var arg = dbStorage.MeasurementArgs
                     .Where(item => item.ClientId == CoreModule.Extension.GetClientId())
                     .OrderByDescending(item => item.RefTime)
                     .FirstOrDefault();

                if (arg != null)
                {
                    this.txtVaporInterval.Value = arg.VaporInterval;
                    this.txtVaporLowValue.Value = arg.VaporLowValue;
                    this.txtVaporHighValue.Value = arg.VaporHighValue;
                }
                else
                {
                    this.txtVaporInterval.Value = 1;
                    this.txtVaporLowValue.Value = 0;
                    this.txtVaporHighValue.Value = 1;
                }
            }
            catch (Exception x)
            {

            }


        }
    }
}
